// 常量配置
export const API_BASE_URL = '/api/'
export const APP_VERSION = '1.2.0'

// 对象形式导出
const ENVConfig = {
	TIMEOUT: 30000,
	ENV: process.env.NODE_ENV === 'development' ? 'dev' : 'prod'
}
export default ENVConfig

/**
 * 检测是否在微信浏览器中打开
 * @returns {boolean} 返回检测结果
 */
export function isWeChatBrowser() {
	try {
		return /micromessenger/i.test(navigator.userAgent);
	} catch (e) {
		console.error('UserAgent检测异常:', e);
		return false;
	}
}

// 扩展方法：检测是否在小程序环境
export function isWeChatMiniProgram() {
	return window.__wxjs_environment === 'miniprogram';
}

export function formatDate(date) {
	// 处理输入为Date对象或时间戳
	const d = date instanceof Date ? date : new Date(date);

	// 获取年月日并补零
	const year = d.getFullYear();
	const month = String(d.getMonth() + 1).padStart(2, '0'); // 月份从0开始
	const day = String(d.getDate()).padStart(2, '0');

	return `${year}-${month}-${day}`;
}

//获取日期今天明天后天 或者星期几
export function getDateText(day, getWeek) {
	//今天的时间
	var day1 = new Date();
	day1.setTime(day1.getTime());
	var month1 = (day1.getMonth() + 1) > 9 ? (day1.getMonth() + 1) : '0' + (day1.getMonth() + 1);
	var date1 = day1.getDate() > 9 ? day1.getDate() : '0' + day1.getDate();
	var s1 = day1.getFullYear() + "-" + month1 + "-" + date1;
	//明天的时间
	var day2 = new Date();
	day2.setTime(day2.getTime() + 24 * 60 * 60 * 1000);
	var month2 = (day2.getMonth() + 1) > 9 ? (day2.getMonth() + 1) : '0' + (day2.getMonth() + 1);
	var date2 = day2.getDate() > 9 ? day2.getDate() : '0' + day2.getDate();
	var s2 = day2.getFullYear() + "-" + month2 + "-" + date2;
	//后天的时间
	var day3 = new Date();
	day3.setTime(day3.getTime() + 2 * 24 * 60 * 60 * 1000);
	var month3 = (day3.getMonth() + 1) > 9 ? (day3.getMonth() + 1) : '0' + (day3.getMonth() + 1);
	var date3 = day3.getDate() > 9 ? day3.getDate() : '0' + day3.getDate();
	var s3 = day3.getFullYear() + "-" + month3 + "-" + date3;
	var text = '';
	if (day == s1) {
		text = '今天';
	} else if (day == s2) {
		text = '明天';
	} else if (day == s3) {
		text = '后天';
	} else {
		text = '';
	}
	if (text == '' && getWeek) {
		var weekDay = new Date(day).getDay()
		if (weekDay == 0) {
			text = '周日'
		}
		if (weekDay == 1) {
			text = '周一'
		}
		if (weekDay == 2) {
			text = '周二'
		}
		if (weekDay == 3) {
			text = '周三'
		}
		if (weekDay == 4) {
			text = '周四'
		}
		if (weekDay == 5) {
			text = '周五'
		}
		if (weekDay == 6) {
			text = '周六'
		}
	}
	return text
}

export function calculateAge(idNum) {
	const birthDateStr = idNum.substr(6, 8);
	const birthYear = parseInt(birthDateStr.substr(0, 4));
	const birthMonth = parseInt(birthDateStr.substr(4, 2)) - 1;
	const birthDay = parseInt(birthDateStr.substr(6, 2));
	const birthDate = new Date(birthYear, birthMonth, birthDay);
	const today = new Date();
	let age = today.getFullYear() - birthYear;
	const monthDiff = today.getMonth() - birthMonth;
	if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDay)) {
		age--;
	}
	return age;
}